#include <stdint.h>

#include "Demuxer.h"

#include "Properties.h"
#include "StrUtil.h"
#include "Log.h"


Demuxer::Demuxer() {}

Demuxer::~Demuxer() {}

bool Demuxer::init(const PropertyBag& props) {
    int64_t start_time = props["start_time"].to<int64_t>(0);
    int64_t start_time_eof = props["start_time_eof"].to<int64_t>(0);
    int64_t stop_time = props["stop_time"].to<int64_t>(INT64_MAX);
    int64_t recording_time = props["recording_time"].to<int64_t>(INT64_MAX);
    std::string filename = props["filename"].toString("");

    if (filename.empty()) {
        LogError("文件名不能为空");
        return false;
    }

    if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
        stop_time = INT64_MAX;
        LogWarn("停止时间和录制时间不能同时使用，使用停止时间");
    }

    if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
        if (stop_time <= start_time) {
            LogError("停止时间不能小于等于开始时间");
            return false;
        } else {
            recording_time = stop_time - start_time;
        }
    }
    
    if (props.contains("format")) {
        auto format = props["format"].toString("").c_str();
        // if (!(file_iformat = av_find_input_format(format))) {
        //     LogFatal("Unknown input format: '%s'\n", format);
        //     return false;
        // }
        LogInfo("format: %s", format);
    }

    // if (!strcmp(filename, "-"))
    //     filename = "fd:";

    // stdin_interaction &= strncmp(filename, "pipe:", 5) &&
    //                      strcmp(filename, "fd:") &&
    //                      strcmp(filename, "/dev/stdin");

    if (filename == "-") {
        filename = "fd:";
    }
    if (/*stdin_interaction*/ 1 && !StrUtil::startWith(filename, "pipe:") &&
        filename != "fd:" && filename != "/dev/stdin") {
        LogInfo("标准输入未开启\n");
    }

    return true;
}